package com.kuaimacode.kframework.api.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuaimacode.kframework.mybatis.mapper.bankcard.BankCardManagementMapper;
import com.kuaimacode.kframework.mybatis.models.bankcard.BankCardManagement;
import com.kuaimacode.kframework.mybatis.models.user.CardBIN;
import com.kuaimacode.kframework.util.BankCardBinUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 12786 on 2019/4/4.
 * @version 1.0
 */
@Service
public class BankCardManagementService extends ServiceImpl<BankCardManagementMapper, BankCardManagement> {

    @Autowired
    private CardBINService cardBINService;

    public List<BankCardManagement> getBankCardList(String banCardNumber){
        QueryWrapper<BankCardManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","1");
        List<BankCardManagement> managements = baseMapper.selectList(queryWrapper);
        List<BankCardManagement> managementList = new ArrayList<>();
        int index = 0;

        CardBIN cardBIN = cardBINService.findByCardNo(banCardNumber);
        if(cardBIN == null){
            return managements;
        }else{
            BankCardManagement otherBank = null;
            for (BankCardManagement management:managements){

                if (cardBIN.getBankName().contains(management.getBankName().replace("银行",""))){
                    management.setBankCardType("借记卡".equals(cardBIN.getType())?"0":"1");
                    managementList.add(management);
                    return managementList;
                }

                if(management.getBankName().equals("其他银行")){
                    otherBank = management;
                }

            }
            otherBank.setBankName(cardBIN.getBankName());
            otherBank.setBankCardType("借记卡".equals(cardBIN.getType())?"0":"1");

            managementList.add(otherBank);
        }

        /*for (BankCardManagement management:managements){
            if (StringUtils.isNotEmpty(banCardNumber)){

                if (StringUtils.isNotEmpty(name)){
                    if (StringUtils.isEmpty(name)){
                        managementList.add(management);
                    }else if (name.contains(management.getBankName().replace("银行",""))){
                        managementList.add(0,management);
                    }else if (name.contains("其他")){
                        managementList.add(0,management);
                    }else{
                        managementList.add(management);
                    }
                }else{
                    managementList.add(management);
                }
            }else{
                managementList.add(management);
            }
        }*/
        return managementList;
    }
}
